<?php

require_once BaseUtil::getModulePath ( "base" ) . '/models/BaseDao.php';

class OrderPackageItemsDao extends BaseDao {
    protected $_name;

    public function _setup() {
    	$this->_name=$this->prefix() . "order_package_items";
    	parent::_setup();
    }
    
    public function getItemsQtyById($itemsid) {
        $select=$this->db_r->select();
        $select->from($this->_name,'sum(order_package_items.quantity) as num');
        $select->join('order_items','order_package_items.order_item_id=order_items.id','*');
        $select->group('order_item_id');  
        $select->where("order_items.is_delete=0");
        $select->where("order_package_items.is_delete=0");
        $select->where('order_items.item_id in(?)',$itemsid);        
        $sql=$select->__toString();
        $value=$this->db_r->fetchOne($sql);
        return $value;
    }
    
    public function getItemNumsById($packageId){
    	$select=$this->db_r->select();
    	$select->from($this->_name, array('order_item_id', 'quantity'));
    	$select->where('order_package_id = ?', $packageId);
    	$select->where("is_delete=0");
    	$sql = $select->__toString();
    	$itemNums = $this->db_r->fetchAll($sql);
    	return $itemNums;
    }
    
    public function getItemsById($packageId){
    	$select = $this->db_r->select();
    	$select->from($this->_name, array('id as item_id', 'order_item_id', 'quantity as qty'));
    	$select->join('order_items', 'order_package_items.order_item_id=order_items.id', 'quantity');
    	$select->join('product_items', 'order_items.item_id=product_items.id', "*");
    	$select->join('products','product_items.product_id=products.id','title');
    	$select->where('order_package_items.order_package_id=?', $packageId);
    	$select->where("order_package_items.is_delete=0");
    	$sql = $select->__toString();
    	$items = $this->db_r->fetchAll($sql);
    	return $items;
    }    
    
    public function addItem($item){
    	return $this->insertData($item);
    }
    
    public function updateItem($id, $item){
    	$where = $this->db_w->quoteInto('id=?', $id);
    	$this->updateData($item, $where);
    }
    
    public function delItem($id){
    	$isDel = array('is_delete'=>1);
    	$where = $this->db_w->quoteInto('order_item_id=?', $id);
    	$this->updateData($isDel, $where);
    }
    
    public function delItemsBypackageId($id){
    	$isDel = array("is_delete"=>1);
    	$where = $this->db_w->quoteInto('order_package_id=?', $id);
    	$this->updateData($isDel, $where);
    }
}